package com.hxzy.system.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hxzy.common.core.domain.entity.SysRole;
import com.hxzy.common.exception.ServiceException;
import com.hxzy.system.domain.dto.SysRoleSearchDTO;
import com.hxzy.system.domain.dto.ValueExistsDTO;
import com.hxzy.system.mapper.SysRoleMapper;
import com.hxzy.system.service.ISysRoleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;

/**
 * 角色
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements ISysRoleService {


    /**
     * 根据角色查询用户角色
     * @param userId 用户ID
     * @return
     */
    @Override
    public Set<String> selectRolePermissionByUserId(Long userId) {
        return this.baseMapper.selectRolePermissionByUserId(userId);
    }

    @Override
    public IPage<SysRole> search(SysRoleSearchDTO sysRoleSearchDTO) {
        IPage<SysRole> page= PageDTO.of(sysRoleSearchDTO.getPageNum(),sysRoleSearchDTO.getPageSize());
        return  this.baseMapper.search(page,sysRoleSearchDTO);
    }

    /**
     * 角色名称是否存在
     * @param valueExistsDTO
     * @return
     */
    @Override
    public int checkRoleNameUnique(ValueExistsDTO valueExistsDTO) {
        int count=this.baseMapper.checkRoleNameUnique(valueExistsDTO);
        return count;
    }

    /**
     * 权限标识是否存在
     * @param valueExistsDTO
     * @return
     */
    @Override
    public int checkRoleKeyUnique(ValueExistsDTO valueExistsDTO) {
        int count=this.baseMapper.checkRoleKeyUnique(valueExistsDTO);
        return count;
    }

    @Override
    public boolean addData(SysRole sysRole) {
        sysRole.setDelFlag("0");
        int count=this.baseMapper.insert(sysRole);
        if(count>0){
            //新增角色的菜单权限 sys_role_menu 新增数据
            if(sysRole.getMenuIds()!=null && sysRole.getMenuIds().length>0){
                count+=this.baseMapper.insertRoleMenu(sysRole.getRoleId(), sysRole.getMenuIds());
            }
        }
        return count>0;
    }

    /**
     * 修改保存角色
     * @param sysRole
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateData(SysRole sysRole) {
        int count=this.baseMapper.updateById(sysRole);
        if(count>0){
            //删除sys_role_menu之前 角色拥有的菜单
            count+=this.baseMapper.deleteRoleMenuByRoleId(sysRole.getRoleId());

            //新增角色的菜单权限 sys_role_menu 新增数据
            if(sysRole.getMenuIds()!=null && sysRole.getMenuIds().length>0){
                count+=this.baseMapper.insertRoleMenu(sysRole.getRoleId(), sysRole.getMenuIds());
            }
        }
        return count>0;
    }

    /**
     * 根据角色ID查询
     * @param roleId
     * @return
     */
    @Override
    public SysRole selectRoleById(Long roleId) {
        return this.baseMapper.selectRoleById(roleId);
    }

    @Override
    public List<SysRole> searchExport(SysRoleSearchDTO sysRoleSearchDTO) {

        return  this.baseMapper.searchExport(sysRoleSearchDTO);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteById(Long[] roleId) {

        //循环遍历每一笔数据，看它是否被使用过
        for(Long id : roleId){
            //判断 sys_user_role表，如果被使用，就不允许删除
            int row=this.baseMapper.sysUserRoleUsed(id);
            if(row>0){
                throw new ServiceException(id+"已分配,不能删除");
            }

             //删除相关的数据  sys_user_role
             this.baseMapper.deleteUserRoleByRoleId(id);
            //删除相关的数据   sys_role_dept
            this.baseMapper.deleteRoleDeptByRoleId(id);
            //删除相关的数据  sys_role_menu
            this.baseMapper.deleteRoleMenuByRoleId(id);

            //更新 sys_role del_flag=2
            SysRole sysRole=new SysRole();
            sysRole.setRoleId(id);
            sysRole.setDelFlag("2");
            this.baseMapper.updateById(sysRole);
        }
        return true;
    }
}
